<template>
    <section>
            <sy-baselist
                    ref="main"
                    :filters="filters"
                    hideOperate
                    hideAdd
                    hidePagination
                    @beforeSearch="beforeSearch"
                    @onLoadData="onLoadData"
                    listUrl="server/report/Report.ashx?cmd=GetAllReport"
            >
                <el-table-column prop="UserName"  align="center"  header-align="center" label="业务员"   width="100" >
                </el-table-column>
                <el-table-column prop="Count0" align="center"  header-align="center" label="电话量"  sortable width="100" >
                    <template scope="scope">
                        <a href="javascript:;" @click="viewTel(scope.row)">{{ scope.row.Count0 }}</a>
                    </template>
                </el-table-column>
                <el-table-column prop="Count4" align="center"  header-align="center" label="有效电话量"  sortable width="90" >
                </el-table-column>
                <el-table-column prop="Count1"  align="center"  header-align="center" label="新增意向客户数"  sortable width="100" >
                    <template scope="scope">
                        <a href="javascript:;" @click="viewAddYxCustomer(scope.row)">{{ scope.row.Count1 }}</a>
                    </template>
                </el-table-column>
                <el-table-column prop="Count2"  align="center" header-align="center" label="已发合同客户数"  sortable width="100" >
                    <template scope="scope">
                        <a href="javascript:;" @click="viewSendContract(scope.row)">{{ scope.row.Count2 }}</a>
                    </template>
                </el-table-column>
                <el-table-column prop="Count3"  align="center"  header-align="center" label="实际签合同客户数" sortable  width="120" >
                    <template scope="scope">
                        <a href="javascript:;" @click="viewSignBill(scope.row)">{{ scope.row.Count3 }}</a>
                    </template>
                </el-table-column>
                <el-table-column prop="note" v-if="showCheckNote" align="center"  header-align="center" label="审核意见"  sortable width="300" >
                </el-table-column>
                <el-table-column v-if="showCheck" align="center"  header-align="center" label="操作"   width="120" >
                    <template scope="scope">
                        <el-button size="small" @click="check(scope.row)">  审核 </el-button>
                    </template>
                </el-table-column>
                <template v-if="teamName" slot="extendbtnTop">
                    <el-button @click="back" type="success" size="small" icon="caret-left">返回</el-button>
                </template>
            </sy-baselist>
            <el-dialog top="5%" title="电话明细" v-model="vTel" :close-on-click-modal="false" size="large">
                <ViewTelTrace ref="telTrace" :fixWhere="telFixWhere">
                </ViewTelTrace>
            </el-dialog>
            <el-dialog top="5%" title="明细" v-model="vTrace" :close-on-click-modal="false" size="small">
                <ViewTrace ref="otherTrace" :fixWhere="otherFixWhere">
                </ViewTrace>
            </el-dialog>
            <el-dialog top="5%" title="审核" v-model="vCheck" :close-on-click-modal="false" size="tiny">
                <el-form ref="formCheck" :model="formCheck" label-width="100px">
                    <el-form-item prop="FRealName" label="姓名">
                        <el-input  :disabled="true"  v-model="formCheck.FRealName" ></el-input>
                    </el-form-item>
                    <el-form-item prop="FCreateDate" label="日期">
                        <el-date-picker :disabled="true" type="date" placeholder="选择日期" v-model="formCheck.FCreateDate"  style="width: 100%"></el-date-picker>
                    </el-form-item>
                    <el-form-item prop="FAvailCount" label="有效电话量">
                        <el-input  v-model.number="formCheck.FAvailCount" type="number"></el-input>
                    </el-form-item>
                    <el-form-item prop="FNote" label="审核意见">
                        <el-input type="textarea" v-model="formCheck.FNote"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click.native="vCheck = false">取 消</el-button>
                    <el-button type="primary" @click="saveCheck" :loading="loginingCheck">保存</el-button>
                </div>
            </el-dialog>
    </section>
</template>

<script>
    import util from '../../common/util'
    import mixin from '../../common/mixin'
    import ViewTelTrace from './ViewTelTrace.vue'
    import ViewTrace from './ViewTrace.vue'
    export default {
        mixins: [mixin],
        components:{
            ViewTelTrace:ViewTelTrace,
            ViewTrace:ViewTrace,
        },
        data(){
            /* //默认本月时间
            let start = util.date.getDate()
            let end =  util.date.getDate()
            start.setDate(1)
            util.date.addMonth(end,1)
            end.setDate(1)
            util.date.addDay(end,-1)
            */
            //默认当天
            let start = util.date.getDate()
            let end =  util.date.getDate()
            return {
                loginingCheck:false,
                formCheck:{},
                vCheck:false,
                showCheck:false,
                showCheckNote:false,
                startDate:start,
                endDate:end,
                teamName:"",
                telFixWhere:"1=2",
                otherFixWhere:"1=2",
                vTrace:false,
                vTel:false,
                filters: [
                          {type: "date",name:"FCreateDate",text:"统计时间", value: [start,end]},
                          {type: "select",name:"FRealName",text:"业务员", data:"server/system/User.ashx?cmd=GetRealNames"}
                ]
            }
        },
        methods:{
            back(){
                this.$router.go(-1)
            },
            beforeSearch(para){
                if(this.teamName){
                    if(para.oWhere){
                        para.oWhere.teamName = this.teamName
                    }
                }
            },
            viewTel(row){
                //TODO:可以看成员的
                let url = "server/report/Report.ashx?cmd=IsTeamLeader"
                util.get(url,{UserName:row.UserName},this.progressFn,result=>{
                    this.vTel = true;
                    let where = this.$refs.main._wherePara().where
                    where = util.joinWhere(where,"FRealName='" + row.UserName + "'")
                    where = util.joinWhere(where,"FTalkTime>1")
                    this.telFixWhere = where
                })
            },
            viewAddYxCustomer(row){
                if(global.user.FID!=-1){
                    return;
                }
                this.vTrace = true;
                let where = this.$refs.main._wherePara().where
                where = util.joinWhere(where,"FRealName='" + row.UserName + "'")
                where = util.joinWhere(where,"FType=1")
                this.otherFixWhere = where
            },
            viewSendContract(row){
                if(global.user.FID!=-1){
                    return;
                }
                this.vTrace = true
                let where = this.$refs.main._wherePara().where
                where = util.joinWhere(where,"FRealName='" + row.UserName + "'")
                where = util.joinWhere(where,"FType=2")
                this.otherFixWhere = where
            },
            viewSignBill(row){
                if(global.user.FID!=-1){
                    return
                }
                this.vTrace = true
                let where = this.$refs.main._wherePara().where
                where = util.joinWhere(where,"FRealName='" + row.UserName + "'")
                where = util.joinWhere(where,"FType=3")
                this.otherFixWhere = where
            },
            check(row){
                let url = "server/report/Report.ashx?cmd=IsTeamLeader"
                util.get(url,{UserName:row.UserName},this.progressFn,result=>{
                    this.formCheck.FID = 0;
                    this.formCheck.FOrgCount = row.Count0;
                    this.formCheck.FAvailCount = row.Count4?row.Count4:0
                    this.formCheck.FNote = row.note
                    this.formCheck.FRealName = row.UserName
                    if (this.filters[0].value && this.filters[0].value[0].getTime() == this.filters[0].value[1].getTime()) {
                        this.formCheck.FCreateDate = this.filters[0].value[0]
                    }
                    this.vCheck = true
                })
            },
            onLoadData(list){
                if (this.filters[0].value && this.filters[0].value[0].getTime() == this.filters[0].value[1].getTime()) {
                    this.showCheck = true
                    this.showCheckNote = true
                } else {
                    this.showCheck = false
                    this.showCheckNote = false
                }
                //11:总经理，13团队负责人
                if(global.user.FID==-1 ||  util.hasRole(11) ||  util.hasRole(13)) {

                }else{
                    this.showCheck = false
                }
            },
            saveCheck(){
                if(this.formCheck.FAvailCount<0){
                    util.msgWarning("有效电话量不能是负数")
                    return
                }
                if(this.formCheck.FOrgCount<this.formCheck.FAvailCount){
                      util.msgWarning("有效电话量不能大于录入电话量")
                      return
                }
                let url = "server/report/ReportCheck.ashx?cmd=Save"
                util.post(url,this.formCheck,this.loadFn("loginingCheck"),result=>{
                    util.msgSuc("审批成功")
                    this.vCheck = false
                    this.$refs.main._refrash()
                })
            }
        },
        created(){
             this.teamName = this.$router.currentRoute.query.teamName
             let startDate = this.$router.currentRoute.query.startDate
             let endDate = this.$router.currentRoute.query.endDate
             if(startDate && endDate){
                  this.startDate = util.date.parse(startDate,"yyyy-MM-dd")
                  this.endDate = util.date.parse(endDate,"yyyy-MM-dd")
                  this.filters[0].value = [this.startDate,this.endDate]
             }
        }
    }
</script>
